#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

class CAnimal
{
public:
	virtual void Display() = 0;

protected:
	string	m_szName;
};

class CHuman : public CAnimal
{
public:
	CHuman()
	{
		m_szName = "Human";
	}
	virtual void Display()
	{
		cout << m_szName << endl;
	}
};

class CDog : public CAnimal
{
public:
	CDog()
	{
		m_szName = "Dog";
	}

	virtual void Display()
	{
		cout << m_szName << endl;
	}
};

class CPig : public CAnimal
{
public:
	CPig()
	{
		m_szName = "Pig";
	}

	virtual void Display()
	{
		cout << m_szName << endl;
	}
};

class CClassFactory
{
public:
	CClassFactory()
	{
	}
	~CClassFactory()
	{
	}
	CAnimal* Create(string szName)
	{
		int nIndex = 0;
		string szClassList[] = {"Human", "Dog", "Pig"};
			
		for (int ii = 0; ii < _countof(szClassList); ii++)
		{
			if (0 == szClassList[ii].compare(szName))
			{
				nIndex = ii;
				break;
			}
		}
		CAnimal* pObj = NULL;
		switch (nIndex)
		{
			case 0:		pObj	= new CHuman;		break;	// Human
			case 1:		pObj	= new CDog;			break;	// Dog
			case 2:		pObj	= new CPig;			break;	// Pig
			default:	pObj	= NULL;
		}
		return pObj;
	}
};

void main()
{
	CClassFactory ClassFactory;
	CAnimal* pHuman = ClassFactory.Create("Human");
	if (NULL != pHuman)
	{
		pHuman->Display();
		delete pHuman;
	}

	CAnimal* pDog = ClassFactory.Create("Dog");
	if (NULL != pDog)
	{
		pDog->Display();
		delete pDog;
	}
	
	CAnimal* pPig = ClassFactory.Create("Pig");
	if (NULL != pPig)
	{
		pPig->Display();
		delete pPig;
	}


	system("pause");
	return;
}